package com.ruanyuan.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ruanyuan.pojo.User;

/**
 * 用户登录状态拦截器
 * 
 * @author zbp
 *
 */
public class LoginIntercepter implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		// 执行完毕后拦截

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		// 处理过程中拦截
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		// 获取session信息
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user==null) {
			System.out.println("你还没有登录噢");
			request.getRequestDispatcher("/WEB-INF/login.jsp");
			return false;
		}
		return false;
	}

}
